﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Project.Database;

namespace Project.Implementations
{
    public class Utils
    {
        public static readonly List<string> ImageExtensions = new List<string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };
        public static string GetIP()
        {
            string VisitorsIPAddr = string.Empty;
            //Users IP Address.                
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                //To get the IP address of the machine and not the proxy
                VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
            {
                VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
            }
            return VisitorsIPAddr;
        }

        public static int GetAdvertType(Advert advert)
        {
            string extension = System.IO.Path.GetExtension(advert.URL).ToUpperInvariant();
            if (ImageExtensions.Contains(extension))
            {
                return 0;
            }
            if (extension.Equals(".SWF"))
            {
                return 1;
            }
            return -1;
        }
        
        public static string GenerateAdHTML(User user, Advert advert, Blog blog)
        {
            string html = string.Empty;

            html += "<a href=\"http://localhost:57334/Click/AdClick?u=" + user.ID + "&a=" + advert.ID + "&b=" + blog.ID + "\">";
            html += "<img src=\"" + advert.URL.Replace("~", "http://localhost:57334") + "\" width=" + advert.AdType.Width + " height=" + advert.AdType.Height + " border=0>";
            html += "</a>";

            /*html += "<script src=\"http://localhost:57334/Scripts/ad.js\"></script>";
            html += "<script>";
            html += "var userID=" + user.ID + ";";
            html += "var advertID=" + advert.ID + ";";
            html += "var blogID=" + blog.ID + ";";
            html += "</script>";
            html += "<img onClick=\"adclick();\"src=\"" + advert.URL.Replace("~", "http://localhost:57334") + "\" width=" + advert.AdType.Width + " height=" + advert.AdType.Length + " border=0>";
            */
            return html;
        }
    }
}